<template>
  <div class="app-container">
    <div class="operation-panel">
      <div class="search-area">
        <el-button type="primary" @click="handleAdd">新建权限</el-button>
      </div>
    </div>

    <el-table
      ref="elTable"
      :data="permissionList"
      row-key="id"
      lazy
      :load="load"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
      style="width: 100%;margin-top:30px;"
    >
      <el-table-column align="center" label="权限名称" width="220">
        <template slot-scope="scope">
          {{ scope.row.name }}
        </template>
      </el-table-column>
      <el-table-column align="left" label="权限标识" width="220">
        <template slot-scope="scope">
          {{ scope.row.slug }}
        </template>
      </el-table-column>
      <el-table-column align="left" label="权限描述">
        <template slot-scope="scope">
          {{ scope.row.description }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="排序">
        <template slot-scope="scope">
          {{ scope.row.sort }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button type="primary" size="small" @click="handleEdit(scope)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDelete(scope)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      layout="prev, pager, next"
      :current-page="pagination.current_page"
      :page-size="pagination.per_page"
      :total="pagination.total"
      @current-change="handleCurrentChange"
    />
    <el-dialog :visible.sync="dialogVisible" :title="dialogType==='edit'?'编辑权限':'新建权限'">
      <el-form :model="permission" label-width="80px" label-position="left">
        <el-form-item label="权限名称">
          <el-input v-model="permission.name" placeholder="权限名称" />
        </el-form-item>
        <el-form-item label="权限标识">
          <el-input v-model="permission.slug" placeholder="权限名称" />
        </el-form-item>
        <el-form-item label="父级菜单">
          <el-cascader
            v-model="permission.parent_id"
            :options="permissionOptions"
            :props="config"
            clearable
          />
        </el-form-item>
        <el-form-item label="排序">
          <el-input v-model="permission.sort" placeholder="排序" />
        </el-form-item>
        <el-form-item label="权限描述">
          <el-input v-model="permission.description" placeholder="描述" />
        </el-form-item>
      </el-form>
      <div style="text-align:right;">
        <el-button type="danger" @click="dialogVisible=false">取消</el-button>
        <el-button type="primary" @click="confirmPermission">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addPermission, deletePermission, getPermissions, updatePermission, getPermissionNodes } from '@/api/permission'
import { deepClone } from '@/utils'
import success from '@/utils/operation-message'

const defaultPermission = {
  name: '',
  slug: '',
  parent_id: 0,
  description: ''
}

export default {
  name: 'Permission',
  data() {
    return {
      permission: Object.assign({}, defaultPermission),
      permissionList: [],
      permissionOptions: [],
      dialogVisible: false,
      dialogType: 'new',
      checkStrictly: false,
      config: { checkStrictly: true, emitPath: false },
      pagination: {
        total: 0,
        current_page: 1,
        per_page: 15
      }
    }
  },
  computed: {
    routesData() {
      return this.routes
    }
  },
  created() {
    this.getPermissions()
    this.getPermissionNodes()
  },
  methods: {
    async getPermissions() {
      const params = {
        parent_id: 0,
        page: this.pagination.current_page,
        per_page: this.pagination.per_page
      }
      const res = await getPermissions(params)
      this.permissionList = res.data
      this.pagination = res.meta.pagination
      return res
    },
    async load(row, treeNode, resolve) {
      const res = await getPermissions({ parent_id: row.id })
      resolve(res.data)
    },
    async getPermissionNodes() {
      const res = await getPermissionNodes()
      this.permissionOptions = res
    },
    handleCurrentChange(val) {
      this.pagination.current_page = val
      this.getPermissions()
    },
    handleAdd() {
      this.permission = Object.assign({}, deletePermission)
      this.dialogType = 'new'
      this.dialogVisible = true
    },
    handleEdit(scope) {
      this.dialogType = 'edit'
      this.dialogVisible = true
      this.permission = deepClone(scope.row)
    },
    handleDelete(scope) {
      this.$confirm('您确定要删除该权限吗?', '', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          await deletePermission(scope.row.id)
          this.getPermissions()
          this.$message({
            type: 'success',
            message: '删除成功'
          })
        })
        .catch(err => { console.error(err) })
    },
    async confirmPermission() {
      const isEdit = this.dialogType === 'edit'
      if (isEdit) {
        this.permission.parent_id = this.permission.parent_id ? this.permission.parent_id : 0
        await updatePermission(this.permission, this.permission.id)
      } else {
        await addPermission(this.permission)
      }

      this.getPermissions()
      this.getPermissionNodes()

      this.$router.replace({
        path: '/redirect' + this.$route.path
      })

      this.dialogVisible = false
      success()
    }
  }
}
</script>

<style lang="scss" scoped>
  .app-container {
    .roles-table {
      margin-top: 30px;
    }
    .permission-tree {
      margin-bottom: 30px;
    }
  }
</style>
